'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    // let result = await this.app.mysql.get("data",{})
    let result = '借口'
    console.log(result)
    this.ctx.body = result
  }
  async getArticleList(){
    let sql = 'select article.id as id, '+
                'article.title as title,'+
                'article.introduce as introduce, '+
                "article.addTime as addTime, "+
                'article.view_count as view_count, '+
                'article.content as content, '+
                'type.typeName as typeName '+
                'from article left join type on article.type_id = type.typeNum '+
                'order by article.id desc'
    const result = await this.app.mysql.query(sql)
    this.ctx.body = {data:result}
  }
  async getArticleById(){
    let id = this.ctx.params.id
    let viewPlusSql = 'update article set article.view_count=article.view_count+1 where article.id=' +id
    const resultView = await this.app.mysql.query(viewPlusSql)
    let sql = 'select article.id as id, '+
                'article.title as title,'+
                'article.introduce as introduce, '+
                "article.addTime as addTime, "+
                'article.view_count as view_count, '+
                'article.content as content, '+
                'type.typeName as typeName '+
                'from article left join type on article.type_id = type.typeNum '+
                'where article.id=' +id
    const result = await this.app.mysql.query(sql)
    this.ctx.body = {data:result}
  }
  //获取类别
  async getTypeInfo(){
    const result = await this.app.mysql.select('type')
    this.ctx.body = {data:result}
  }
  //根据类别Id获取文章列表
  async getListById(){
    let id = this.ctx.params.id
    let sql = 'select article.id as id, '+
    'article.title as title,'+
    'article.introduce as introduce, '+
    "article.addTime as addTime, "+
    'article.view_count as view_count, '+
    'article.content as content, '+
    'type.typeName as typeName '+
    'from article left join type on article.type_id = type.typeNum '+
    'where article.type_id=' +id+
    ' order by article.id desc'
    const result = await this.app.mysql.query(sql)
    this.ctx.body = {data:result}
  }
  async getMessageById(){
    let id = this.ctx.params.id
    let sql = 'select articleId as articleId, '+
    'name as leaveName,'+
    'message as leaveMessage, '+
    "time as time "+
    'from leaveMessage '+
    'where articleId = '+ id +
    ' and whether = '+ 1 +
    ' order by id desc'
    const result = await this.app.mysql.query(sql)
    this.ctx.body = {list:result}
  }
  //加留言的接口
  async addLeaveMassage(){
    let tmpMassage = this.ctx.request.body
    console.log(tmpMassage)
    const result = await this.app.mysql.insert('leaveMessage',tmpMassage)
    const insertSuccess = result.affectedRows === 1
    const insertId = result.insertId
    this.ctx.body = {
        isSuccess:insertSuccess,
        insertId:insertId,
    }
  }
}

module.exports = HomeController;

//RESTful是一种设计架构，在前后端分离的项目中经常出现
//egg-mysql,链接数据库